package fr.isen.smssender.sms;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import fr.isen.smssender.R;
import fr.isen.smssender.database.DBManager;
import fr.isen.smssender.contact.*;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class Parameter extends Activity
{

    private TextView mDateDisplay;
    private int mYear;
    private int mMonth;
    private int mDay;

    static final int DATE_DIALOG_ID = 1;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.parameters);
        // récupérer le nom du contact et le sms
        //Contact contact = getIntent().getAction("contact",contact);
        //Sms sms= getIntent().getAction("sms",sms);
        DBManager dbManager = new DBManager(this);
        ArrayList<Contact> contactArrayList = dbManager.getContacts();
        ArrayAdapter<String> datas = new ArrayAdapter<String>(this, R.layout.sms_list_item,R.id.listitem);
        for(Contact contact : contactArrayList){
            datas.add("Nom: "+contact.getNom()+ " \nPrénom: "+contact.getPrenom()+ " \nNuméro: "+contact.getNumber());
        }
        Spinner contactSpinner = (Spinner)findViewById(R.id.spinnerContact);
        contactSpinner.setAdapter(datas);

        ArrayList<Sms> smsArrayList = dbManager.getSms();
        ArrayAdapter<String> smsDatas = new ArrayAdapter<String>(this, R.layout.sms_list_item,R.id.listitem);
        for(Sms sms : smsArrayList){
            smsDatas.add("Message: "+sms.getMessage());
        }
        smsDatas.add("Autre");
        Spinner smsSpinner = (Spinner)findViewById(R.id.spinnerSms);
        smsSpinner.setAdapter(smsDatas);
        smsSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String smsString = (String) adapterView.getItemAtPosition(i).toString();
                EditText newSms = (EditText)findViewById(R.id.editTextSms);
                if(smsString.equalsIgnoreCase("Autre")){

                    newSms.setVisibility(EditText.VISIBLE);
                } else{
                    newSms.setVisibility(EditText.INVISIBLE);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });


        //afficher dans l'edittext le contact choisi, id de l'edittext: editTextContact
        // meme chose mais le sms choisi, id de l'edittext: editTextSms


        // pour le date picker
        mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
        Button pickDate = (Button) findViewById(R.id.pickDate);
        pickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });

        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
        updateDisplay();




        Button SmsRecorder= (Button)findViewById(R.id.register);
        SmsRecorder.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                newSms();
            }
        });



    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG_ID:
                return new DatePickerDialog(this,
                        mDateSetListener,
                        mYear, mMonth, mDay);
        }
        return null;
    }

    protected void onPrepareDialog(int id, Dialog dialog) {
        switch (id) {
            case DATE_DIALOG_ID:
                ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
                break;
        }
    }
    private void updateDisplay() {
        mDateDisplay.setText(
                new StringBuilder()
                        // Month is 0 based so add 1
                        .append(mMonth + 1).append("-")
                        .append(mDay).append("-")
                        .append(mYear).append(" "));
    }
    private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker view, int year, int monthOfYear,
                                      int dayOfMonth) {
                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    updateDisplay();
                }
            };

    private void newSms (){
        DBManager dbManager = new DBManager(this);
        ArrayList<Contact> contacts = dbManager.getContacts();
        Contact contactToSend = null;
        String name;
        String firstName;
        String number;
        Date date = new Date(mYear,mMonth,mDay);
        String stringToParse = (((Spinner)findViewById(R.id.spinnerContact)).getSelectedItem().toString());
        name = stringToParse.split(" ")[1];
        firstName = stringToParse.split(" ")[3];
        number = stringToParse.split(" ")[5];
        Sms sms = null;
        for(Contact contact : contacts){
            if(contact.getNom().equalsIgnoreCase(name) && firstName.equalsIgnoreCase(contact.getPrenom()) && contact.getNumber().equalsIgnoreCase(number) ){
                contactToSend = contact;
            }
        }
        if(contactToSend != null){  

            if(((EditText)findViewById(R.id.editTextSms)).getVisibility() == EditText.VISIBLE){
                sms = new Sms(((EditText)findViewById(R.id.editTextSms)).getText().toString());
                dbManager.setSms(sms);
            }else{
                String message =(((Spinner)findViewById(R.id.spinnerSms)).getSelectedItem().toString().split(":")[((Spinner)findViewById(R.id.spinnerSms)).getSelectedItem().toString().split(":").length -1]);
                sms = new Sms(message);
                ArrayList<Sms> listSms = dbManager.getSms();
                for(Sms newSms : listSms){
                    if(newSms.getMessage().equals(" "+message)){
                        sms.set_id(newSms.get_id());
                    }
                }
            }


        }   
        if(sms.get_id() != 0 && contactToSend.get_id()!= 0)
        dbManager.setSmsToSend(contactToSend,sms,date);

    }
}